<?php
/**
 * Created by PhpStorm.
 * User: hui
 * Date: 2017/2/19
 * Time: 21:50
 */
namespace app\admin\logic;

use think\Db;
use think\Loader;
use think\Model;

class Admin extends Model
{
    /**
     * 用户权限表
     * by kuddy
     * @return array
     */
    public function authList()
    {
        $admin = Loader::model('admin');
        //管理员ID
        $id = session('admin');
        //所属用户组
        $roles = $admin->where('id', $id)->find()->roles()->select();
        $list = '';
        //遍历用户组，生成权限字符串
        if (is_array($roles)) {
            foreach ($roles as $role) {
                $list .= $role->role_act_list . ',';
            }
        }
        //去掉最后的逗号
        $list = substr($list, 0, -1);
        //拼合成数组
        $list = explode(',', $list);
        return array_flip($list);
    }

    /**
     * 当前请求ID
     * by kuddy
     * @param $param
     * @return array|false|\PDOStatement|string|Model
     */
    public function actionId($param)
    {
        $map = [
            'module'     => $param[0],
            'controller' => $param[1],
            'action'     => $param[2],
        ];
        return Db::name('admin_module')
            ->field('module_id')
            ->where($map)
            ->find();
    }

    public function add()
    {
        $admin = Loader::model('admin');
        return $admin->allowField(true)->save(input('post.'));
    }
}